/*
 * FreeRTOS Kernel <DEVELOPMENT BRANCH>
 * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * SPDX-License-Identifier: MIT
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * https://www.FreeRTOS.org
 * https://github.com/FreeRTOS
 *
 */

/*
 * This is a simple main that will start the FreeRTOS-Kernel and run a periodic task
 * that only delays if compiled with the template port, this project will do nothing.
 * For more information on getting started please look here:
 * https://freertos.org/FreeRTOS-quick-start-guide.html
 */

/* FreeRTOS includes. */
#include <FreeRTOS.h>
#include <task.h>
#include <queue.h>
#include <timers.h>
#include <semphr.h>

/* Standard includes. */
#include <stdio.h>
#include "ns16550.h"
#include "printf.h"

extern void freertos_risc_v_trap_handler( void );

/*-----------------------------------------------------------*/

static void exampleTask( void * parameters ) __attribute__( ( noreturn ) );

/*-----------------------------------------------------------*/

static void exampleTask( void * parameters )
{
	/* Unused parameters. */
	( void ) parameters;
	static uint8_t i;

	for( ; ; )
	{
		i++;

		printf("run 0: test i:%d\n", i);
		vTaskDelay( 100 ); /* delay 100 ticks */
	}
}

static void exampleTask_1( void * parameters )
{
	/* Unused parameters. */
	( void ) parameters;
	static uint8_t i;
	for( ; ; )
	{
		i++;

		printf("run 1: test i:%d\n", i);
		vTaskDelay( 100 ); /* delay 100 ticks */
	}
}
/*-----------------------------------------------------------*/

int main( void )
{

	 __asm__ volatile ( "csrw mtvec, %0" : : "r" ( freertos_risc_v_trap_handler ) );

	(void)ns16550_init();
	ns16550_puts("Example FreeRTOS Project\n" );
	printf("%s \n", "Example FreeRTOS Project");



	xTaskCreate( exampleTask,             /* The function that implements the task. */
					"example",                            /* The text name assigned to the task - for debug only as it is not used by the kernel. */
					configMINIMAL_STACK_SIZE,        /* The size of the stack to allocate to the task. */
					NULL,                            /* The parameter passed to the task - not used in this simple case. */
					configMAX_PRIORITIES - 1U, /* The priority assigned to the task. */
					NULL );                          /* The task handle is not required, so NULL is passed. */

	xTaskCreate( exampleTask_1,             /* The function that implements the task. */
					"example1",                            /* The text name assigned to the task - for debug only as it is not used by the kernel. */
					configMINIMAL_STACK_SIZE,        /* The size of the stack to allocate to the task. */
					NULL,                            /* The parameter passed to the task - not used in this simple case. */
					configMAX_PRIORITIES - 1U, /* The priority assigned to the task. */
					NULL );                          /* The task handle is not required, so NULL is passed. */

	/* Start the scheduler. */
	vTaskStartScheduler();

	for( ; ; )
	{
		/* Should not reach here. */
	}

	return 0;
}
/*-----------------------------------------------------------*/

#if ( configCHECK_FOR_STACK_OVERFLOW > 0 )

	void vApplicationStackOverflowHook( TaskHandle_t xTask,
										char * pcTaskName )
	{
		/* Check pcTaskName for the name of the offending task,
		 * or pxCurrentTCB if pcTaskName has itself been corrupted. */
		( void ) xTask;
		( void ) pcTaskName;
	}

#endif /* #if ( configCHECK_FOR_STACK_OVERFLOW > 0 ) */
/*-----------------------------------------------------------*/
